/**
 * Copyright© 2003-2013 浙江汇信科技有限公司, All Rights Reserved <br/>
 * 描述: 业务文件 <br/>
 * @author 胡义振
 * @date 2013-08-06
 * @version 1.0
 */
package com.scm.biz.service._impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scm.biz.bean.Resource;
import com.scm.biz.bean.SysUser;
import com.scm.biz.dao.SysUserDao;
import com.scm.biz.query.SysUserQuery;
import com.scm.biz.result.SysUserInfoResult;
import com.scm.biz.result.SysUserResult;
import com.scm.biz.service.ResourceService;
import com.scm.biz.service.SysUserService;

/**
 * 
 * 描述: 系统用户Service实现<br>
 * @author 胡义振
 * @date 2013-08-06
 */
@Service
public class SysUserServiceImpl  extends AuthorizingRealm implements SysUserService 
{
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	ResourceService resourceService;
	public List<SysUserResult> queryPage(SysUserQuery sysUserQuery){
		sysUserQuery.setRecourdCount(sysUserDao.selectPageCount(sysUserQuery));
		return sysUserDao.selectPage(sysUserQuery);
	}
	@Override
	public List<SysUser> queryList() {
		return sysUserDao.selectList();
	}
	@Override
	public void insert(SysUser sysUser) {
		sysUserDao.insert(sysUser);
	}
	@Override
	public void updateByPrimaryKeySelective(SysUser sysUser) {
		sysUserDao.updateByPrimaryKeySelective(sysUser);
	}
	@Override
	public SysUser selectByEmail(String email) {
		return sysUserDao.selectByEmail(email);
	}
	@Override
	public SysUser selectByPrimaryKey(int id) {
		return sysUserDao.selectByPrimaryKey(id);
	}
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
		//获取登录时输入的用户名  
        String loginName=(String) principalCollection.fromRealm(getName()).iterator().next();  
        //到数据库查是否有此对象  
        SysUser user=sysUserDao.selectByEmail(loginName);
        if(user!=null){  
            //权限信息对象info,用来存放查出的用户的所有的角色（role）及权限（permission）  
            SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();  
            //用户的角色集合  
            //info.setRoles(user.getRolesName());  
            //用户的角色对应的所有权限，如果只使用角色定义访问权限，下面的四行可以不要  
            //List<Role> roleList=user.getRoleList();  
        
            for (Resource resource : resourceService.selectListByUserId(user.getId())) {  
                info.addStringPermission(resource.getCode());  
            }  
            return info;  
        }  
        return null;  
	}
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken authenticationToken) throws AuthenticationException {
		 //UsernamePasswordToken对象用来存放提交的登录信息  
		
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;  
        
        //查出是否有此用户   
        SysUser user=sysUserDao.selectByEmail(token.getUsername()); 
        if(user!=null){  
            //若存在，将此用户存放到登录认证info中  
            return new SimpleAuthenticationInfo(user.getEmail(), user.getPassword(), getName());  
        }  
        return null;  
	}
	@Override
	public void delete(int id) {
		sysUserDao.deleteByPrimaryKey(id);
	}
	@Override
	public SysUserInfoResult getUserById(String email) {
		SysUserInfoResult sysUserInfoResult = new SysUserInfoResult();
		sysUserInfoResult = sysUserDao.selectUserById(email);
		List<String> roleName = sysUserDao.selectRoleName(sysUserInfoResult.getId());
		sysUserInfoResult.setRoleName(roleName);
		return sysUserInfoResult;
	}
	@Override
	public int modifyPassword(String email,String password) {
		SysUser sysUser = new SysUser();
		sysUser.setEmail(email);
		sysUser.setPassword(password);
		sysUser.setUpdatedAt(new Date());
		int i = sysUserDao.updatePassword(sysUser);
		return i;
	}
	@Override
	public String getPassword(String email) {
		String result = sysUserDao.selectPassword(email);
		return result;
	}
	@Override
	public List<SysUser> selectListByRoleCode(String roleCode) {
		return sysUserDao.selectListByRoleCode(roleCode);
	}
	@Override
	public List<SysUser> selectList() {
		return sysUserDao.selectList();
	}

	@Override
	public List<SysUser> getManager() {
		return sysUserDao.selectManager();
	}
	@Override
	public List<SysUser> getSale() {
		return sysUserDao.selectSale();
	}
	@Override
	public boolean haveEmail(String email, Long id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("email", email);
		map.put("id", id);
		int num = sysUserDao.haveEmail(map);
		if(num!=0){
			return true;
		}
		return false;
	}
	
}
